﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：DefaultInstance.cs
// 功能描述：TongTongMall.MallWeb.Core
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using Castle.Core.Logging;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TEC.Public.Component.UMengAopSdk;

using TongTongMall.Configuration;
using TongTongMall.Dependecy;
using TongTongMall.MallWeb.Core.Instance.Config;
using TongTongMall.Threading;

namespace TongTongMall.MallWeb.Core.Instance
{
    public class DefaultInstance : IInstance, ISingletonDependency
    {
        public static DefaultInstance Instance { get { return SingletonInstance; } }

        private static readonly DefaultInstance SingletonInstance = new DefaultInstance();

        /// <summary>
        /// 日志助手
        /// todo（jym，统一由log4net进行记录，记录到logfile/alipaylog内）
        /// </summary>
        public ILogger AliPayLoggerHelper
        {
            get
            {
                return AlipayLogHelper.Logger;
            }
        }

        //public UploadConfig UploadConfig
        //{
        //    get
        //    {
        //        return TEC.Public.Infrastructure.Config.ConfigHelper.Instance.GetConfig<UploadConfig>();
        //    }
        //}

        public string GetMallSetting(long tenantID, string key)
        {
            var _settingManager = IocManager.Instance.Resolve<ISettingManager>();

            var settings = AsyncHelper.RunSync(() =>
            {
                return _settingManager.GetSettingValueForTenantAsync(key, tenantID);
            });
            return settings;
        }
        public string GetMallSetting(string key)
        {
            var _settingManager = IocManager.Instance.Resolve<ISettingManager>();

            var settings = AsyncHelper.RunSync(() =>
            {
                return _settingManager.GetSettingValueForApplicationAsync(key);
            });
            return settings;
        }
        #region 推送

        #region MessagePushDebugMode
        private bool _MessagePushDebugMode;
        public bool MessagePushDebugMode
        {
            get
            {

                if (_AndroidMessagePushClient == null)
                {
                    try
                    {
                        var _settingManager = IocManager.Instance.Resolve<ISettingManager>();

                        var settings = AsyncHelper.RunSync(() => { return _settingManager.GetSettingValueForApplicationAsync("MessagePushDebugMode"); });
                        var config = Convert.ToBoolean(settings);
                        return config;
                    }
                    catch (Exception ex)
                    {
                        Logging.LogHelper.Logger.Fatal($"推送模式默认值初始化出错", ex);
                    }
                }
                return true;
            }
        }
        #endregion

        #region AndroidMessagePushConfig
        private IAopClient _AndroidMessagePushClient;
        public IAopClient AndroidMessagePushClient
        {
            get
            {
                if (_AndroidMessagePushClient == null)
                {
                    try
                    {
                        var _settingManager = IocManager.Instance.Resolve<ISettingManager>();

                        var settings = AsyncHelper.RunSync(() => { return _settingManager.GetSettingValueForApplicationAsync("AndroidMessagePushConfig"); });
                        var config = JsonConvert.DeserializeObject<MessagePushConfig>(settings);

                        _AndroidMessagePushClient = new DefaultAopClient(config.ServerUrl, config.AppKey, config.AppMasterSecret);
                    }
                    catch (Exception ex)
                    {
                        Logging.LogHelper.Logger.Fatal($"Android推送实例对象初始化出错", ex);
                    }
                }
                return _AndroidMessagePushClient;
            }
        }
        #endregion

        #region IOSMessagePushClient
        private IAopClient _IOSMessagePushClient;
        public IAopClient IOSMessagePushClient
        {
            get
            {
                if (_IOSMessagePushClient == null)
                {
                    try
                    {
                        var _settingManager = IocManager.Instance.Resolve<ISettingManager>();

                        var settings = AsyncHelper.RunSync(() => { return _settingManager.GetSettingValueForApplicationAsync("IOSMessagePushConfig"); });
                        var config = JsonConvert.DeserializeObject<MessagePushConfig>(settings);

                        _IOSMessagePushClient = new DefaultAopClient(config.ServerUrl, config.AppKey, config.AppMasterSecret);
                    }
                    catch (Exception ex)
                    {
                        Logging.LogHelper.Logger.Fatal($"IOS推送实例对象初始化出错", ex);
                    }
                }
                return _IOSMessagePushClient;
            }
        }
        #endregion

        #endregion

        //private IDictionary<Order.Enum.WaybillCompanyEnum, IKuaiDiQuery> _KuaiDiQueryManage;
        //public IDictionary<Order.Enum.WaybillCompanyEnum, IKuaiDiQuery> KuaiDiQueryManage
        //{
        //    get
        //    {
        //        if (_KuaiDiQueryManage != null) return _KuaiDiQueryManage;

        //        var _settingManager = IocManager.Instance.Resolve<ISettingManager>();
        //        var shenTongsettings = AsyncHelper.RunSync(() => { return _settingManager.GetSettingValueForApplicationAsync("ShenTongQueryConfig"); });
        //        var sheTongConfig = JsonConvert.DeserializeObject<ShenTongQueryConfig>(shenTongsettings);

        //        _KuaiDiQueryManage = new Dictionary<Order.Enum.WaybillCompanyEnum, IKuaiDiQuery>();
        //        //_KuaiDiQueryManage[Order.Enum.WaybillCompanyEnum.ShenTong] = new ShenTongKuaiDiQuery(Instance.ApiConfig.ShenTongQueryConfig);
        //        _KuaiDiQueryManage[Order.Enum.WaybillCompanyEnum.ShenTong] = IocManager.Instance.Resolve<DefaultShenTongKuaiDiQuery>(new
        //        {
        //            config = sheTongConfig
        //        });
        //        return _KuaiDiQueryManage;
        //    }
        //}
    }
}
